package com.five.util;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 适配 ES 6.8.6 的索引创建工具（包含 type 定义）
 */
@Component
public class NovelIndexInitializer {
    public static final String INDEX_NAME = "novel_index";
    public static final String TYPE_NAME = "_doc"; // 6.x 需指定 type

    public static void createIndex(RestHighLevelClient client) throws IOException {
        // 1. 索引设置（分片、副本、分词器）
        Settings settings = Settings.builder()
                .put("number_of_shards", 3)
                .put("number_of_replicas", 1)
                .put("analysis.analyzer.ik_max_word_analyzer.type", "ik_max_word")
                .put("analysis.analyzer.ik_smart_analyzer.type", "ik_smart")
                .build();

        // 2. 字段映射（6.x 需在 mappings 中指定 type）
        XContentBuilder mapping = XContentFactory.jsonBuilder()
                .startObject()
                    .startObject(TYPE_NAME) // 指定 type 为 _doc
                        .startObject("properties")
                            // 小说ID
                            .startObject("id")
                                .field("type", "keyword")
                            .endObject()
                            // 书名（核心搜索字段）
                            .startObject("bookTitle")
                                .field("type", "text")
                                .field("analyzer", "ik_max_word_analyzer")
                                .field("search_analyzer", "ik_max_word_analyzer")
                                .field("boost", 3.0)
                            .endObject()
                            // 作者名
                            .startObject("authorName")
                                .field("type", "text")
                                .field("analyzer", "ik_max_word_analyzer")
                                .field("search_analyzer", "ik_max_word_analyzer")
                                .field("boost", 2.5)
                                .startObject("fields")
                                    .startObject("keyword")
                                        .field("type", "keyword")
                                    .endObject()
                                .endObject()
                            .endObject()
                            // 分类名
                            .startObject("categoryName")
                                .field("type", "text")
                                .field("analyzer", "ik_max_word_analyzer")
                                .field("search_analyzer", "ik_max_word_analyzer")
                                .field("boost", 2.0)
                                .startObject("fields")
                                    .startObject("keyword")
                                        .field("type", "keyword")
                                    .endObject()
                                .endObject()
                            .endObject()
                            // 简介
                            .startObject("introduction")
                                .field("type", "text")
                                .field("analyzer", "ik_max_word_analyzer")
                                .field("search_analyzer", "ik_max_word_analyzer")
                                .field("boost", 1.5)
                            .endObject()
                            // 标签
                            .startObject("tags")
                                .field("type", "text")
                                .field("analyzer", "ik_smart_analyzer")
                                .field("search_analyzer", "ik_smart_analyzer")
                                .field("boost", 1.0)
                            .endObject()
                            // 其他字段
                            .startObject("status").field("type", "byte").endObject()
                            .startObject("score").field("type", "double").endObject()
                            .startObject("wordCount").field("type", "long").endObject()
                            .startObject("createTime")
                                .field("type", "date")
                                .field("format", "yyyy-MM-dd HH:mm:ss")
                            .endObject()
                            .startObject("updateTime")
                                .field("type", "date")
                                .field("format", "yyyy-MM-dd HH:mm:ss")
                            .endObject()
                        .endObject()
                    .endObject()
                .endObject();

        // 3. 创建索引请求（6.x 需指定索引名）
        CreateIndexRequest request = new CreateIndexRequest(INDEX_NAME);
        request.settings(settings);
        request.mapping(TYPE_NAME, mapping); // 绑定 type 和 mapping

        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        if (response.isAcknowledged()) {
            System.out.println("索引创建成功（6.8.6）：" + INDEX_NAME);
        } else {
            throw new RuntimeException("索引创建失败");
        }
    }
}